package cate.game.activity.specall;

import cate.common.table.activity.specall.row.SpeCallExchangeRow;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.specall.msg.SpeCallExchangeDataResp;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass("独立卡池召唤-兑换数据")
public class SpeCallExchange extends SubActivitySingle<SpeCallActivity> {
    @NoteField(value = "礼包商品信息")
    public Map<Integer, SpeCallExchangeItem> items;

    @Override
    public void initialize(SpeCallActivity parent) {
        super.initialize(parent);
        if (items == null) {
            items = new HashMap<>();
        }
    }

    @Override
    public void onEnter(){
        super.onEnter();
        checkItems();
    }

    public void onStart(){
        checkItems();
    }

    private void checkItems(){
        List<Integer> list = role.getGame().table.speCall.exchange.getRow(parent.configTid());
        for (Integer itemTid : list) {
            if (items.containsKey(itemTid)) {
                continue;
            }
            SpeCallExchangeItem item = new SpeCallExchangeItem(itemTid);
            items.put(itemTid, item);
        }
        items.entrySet().removeIf(e -> !list.contains(e.getKey()));
    }

    /**
     * 购买补给商品
     *
     * @param tid    商品ID
     * @param num        数量
     * @return 购买结果 无携带数据
     */
    public EcResult<Void> exchangeItem(int tid, int num) {
        // 基础检测
        EcResult<Void> r = new EcResult<>();
        if (!items.containsKey(tid)) {
            return r.fail("找不到商品");
        }
        if (num <= 0) {
            return r.fail("数量错误");
        }
        // 商品购买条件检测
        SpeCallExchangeItem item = items.get(tid);
        EcResult<Void> checkRes = item.checkBuyAllow(role.getGame(), num);
        if (!checkRes.ok()) {
            return checkRes;
        }
        SpeCallExchangeRow itemConfig = item.getConfigRow(role.getGame());
        if (itemConfig == null) {
            return r.fail("找不到商品");
        }
        MixRes costRes = new MixRes(itemConfig.consumeStr).multiply(num);
        if (!costRes.isEmpty()) {
            EcResult<MixResItem> consumeRes = costRes.consume(role, null);
            if (!consumeRes.ok()) {
                role.getGame().notice.tipResItem(role, consumeRes.data);
                return r.fail(consumeRes.message);
            }
        }
        // 发放奖励
        MixRes rewardRes = new MixRes(itemConfig.rewardStr).multiply(num);
        item.addBuyNum(num);
        rewardRes.add(role, true);
        role.getGame().notice.dialogReward(role, rewardRes);
        noticeUpdate();
        return r.success();
    }

    public void clear() {
        items.clear();
    }

    public void noticeUpdate(){
        role.sendNow(new SpeCallExchangeDataResp(this));
    }

    @Override
    public void onDaySpan(boolean silence){
        List<SpeCallExchangeRow> list = role.getGame().table.speCall.exchange.getRows(parent.configTid());
        for (SpeCallExchangeRow row : list) {
            if (row.refreshType == 1) {
                SpeCallExchangeItem item = items.get(row.id);
                if (item != null) {
                    item.resetBuyNum();
                }
            }
        }
    }
}
